package engine

import (
	"github.com/gonutz/go-sdl2/sdl"
)

type State interface {
	Enter()
	Exit()
	HandleEvent(e sdl.Event)
	Draw()
}

type GameEngine interface {
	Quit()
	ChangeState(to State)
}

type Engine struct {
	running bool
	state   State
}

func NewEngine() *Engine {
	return &Engine{}
}

func (e *Engine) Run()          { e.running = true }
func (e *Engine) Running() bool { return e.running }
func (e *Engine) Quit()         { e.running = false }

func (e *Engine) ChangeState(to State) {
	e.exitState()
	e.state = to
	e.enterState()
}

func (e *Engine) enterState() {
	if e.state != nil {
		e.state.Enter()
	}
}

func (e *Engine) exitState() {
	if e.state != nil {
		e.state.Exit()
	}
}

func (e *Engine) HandleEvent(event sdl.Event) {
	if e.state != nil {
		e.state.HandleEvent(event)
	}
}

func (e *Engine) Draw() {
	e.state.Draw()
}
